<?php

namespace App\Http\Controllers\Admin\Goods;

use App\Http\Controllers\Controller;
use App\Serves\CategoryServe;
use App\Utils\Show;
use Illuminate\Http\Request;
use App\Utils\Category;

use App\Models\Goods\Category as CategoryModel ;

class CategoryController extends Controller
{
    public function add(Request $request){
        $params = $request->all();
        $data = [
            'pid'=>$params['parentid'],
            'name'=>$params['name'],
            'status'=>$params['status'],
            'weigh'=>$params['weigh'],
        ];
        $res = CategoryServe::add($data);
//        var_dump($res);
        if($res) return Show::success();
        return Show::error();
    }

    public function edit(Request $request){
        $params = $request->all();
        $data = [
            'id'=>$params['id'],
            'pid'=>$params['parentid'],
            'name'=>$params['name'],
            'status'=>$params['status'],
            'weigh'=>$params['weigh'],
        ];
        $res = CategoryServe::save($data);
        if($res) return Show::success();
        return Show::error();
    }

    public function del(Request $request){
        $params = $request->all();
        $res = CategoryServe::del($params['id']);
        if($res) return Show::success();
        return Show::error();
    }

    public function lists(){
        $result = CategoryModel::all();
        $data = [
            "code"=> 0,
            "msg"=> "",
            "count"=> count($result),
            "data"=>$result
        ];
        return json_encode($data);
    }

    public function lists1(){
        $result1 = CategoryModel::all();
        $result = Category::unlimitedForLevel2($result1,'--',0,2);
        $data = [
            "code"=> 0,
            "msg"=> "",
            "count"=> count($result),
            "data"=>$result
        ];
        return json_encode($data);
    }

    public function getTree(){
        $data = CategoryModel::all()->toArray();
        $data = reArray($data);
        $tree = Category::getTree($data);
//       var_dump($tree);
        return showMsg(200,'ok',$tree);
    }



}
